package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.MyException.InsufficientQuantity;
import com.tenton.pojo.ExCommodity;
import com.tenton.pojo.ImCommodity;
import com.tenton.service.ExCommodityService;
import com.tenton.service.ImCommodityService;
import com.tenton.service.StockService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("员工商品退货管理")
@RequestMapping("/employee/flatform/exCommodity")
public class ExCommodityController {
    @Autowired
    ExCommodityService exCommodityService;
    @Autowired
    ImCommodityService imCommodityService;
    @Autowired
    StockService stockService;

    /**
     * 获取所有退货信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有退货信息")
    @GetMapping("/getAll")
    public String getAll(ModelMap model,
                         @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        exCommodityService.getAll(pn, model);
        return "employee/commodity/getExCommodityList";
    }

    /**
     * 根据id查询退货信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("根据id查询退货信息")
    @PostMapping("/getExCommodity")
    public String getExCommodity(int id, Model model) {
        exCommodityService.selectByPrimaryKey(id, model);
        return "employee/commodity/getExCommodity";
    }

    /**
     * 时间格式化
     *
     * @param binder
     */
    @InitBinder
    protected void init(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    /**
     * 商品名称模糊查询
     *
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("商品名称模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(ModelMap model,
                              @ApiParam("商品名称") @RequestParam(value = "comName", required = false) String comName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        exCommodityService.getByParams(comName, pn, model);
        return "employee/commodity/getExCommodityByParams";

    }

    /**
     * 根据id查询进货信息
     *
     * @param imId
     * @return
     */
    @ApiOperation("根据id查询进货信息")
    @PostMapping("/getExCommodities")
    public ImCommodity imCommodity(int imId) {
        return imCommodityService.selectById(imId);
    }
}

